import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class BillController {
    // 创建BillService对象
    private BillService billService = new BillService();
    // 创建Scanner对象
    private Scanner scanner = new Scanner(System.in);

    // 启动方法
    public void start() {
        while (true) {
            // 显示菜单
            showMenu();
            // 获取用户输入
            int choice = Integer.parseInt(scanner.nextLine());
            // 根据用户输入执行相应操作
            switch (choice) {
                case 1:
                    recordIncome();
                    break;
                case 2:
                    recordExpense();
                    break;
                case 3:
                    viewAllBills();
                    break;
                case 4:
                    queryBills();
                    break;
                case 5:
                    setMonthlyBudget();
                    break;
                case 6:
                    getMonthlyReport();
                    break;
                case 7:
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("无效选项，请重新选择");
            }
        }
    }

    // 显示菜单方法
    private void showMenu() {
        System.out.println("=================================");
        System.out.println("欢迎使用个人账单管理系统");
        System.out.println("=================================");
        System.out.println("请选择操作：");
        System.out.println("1. 记录收入");
        System.out.println("2. 记录支出");
        System.out.println("3. 查看所有账单");
        System.out.println("4. 查询账单");
        System.out.println("5. 设置月度预算");
        System.out.println("6. 查看月度统计报告");
        System.out.println("7. 退出系统");
        System.out.print("请输入选项序号：");
    }

    // 记录收入方法
    private void recordIncome() {
        System.out.print("日期（YYYY-MM-DD）：");
        String date = scanner.nextLine();
        System.out.print("金额：");
        double amount = Double.parseDouble(scanner.nextLine());
        System.out.print("类别（如工资、奖金等）：");
        String category = scanner.nextLine();
        System.out.print("备注：");
        String note = scanner.nextLine();
        try {
            // 调用BillService对象的recordIncome方法记录收入
            billService.recordIncome(date, amount, category, note);
            System.out.println("收入已成功记录！");
        } catch (SQLException e) {
            System.out.println("记录收入时发生错误：" + e.getMessage());
        } catch (IllegalArgumentException e) {
            System.out.println("输入错误：" + e.getMessage());
        }
        System.out.println("按任意键返回主菜单..");
        scanner.nextLine();
    }

    // 记录支出方法
    private void recordExpense() {
        System.out.print("日期（YYYY-MM-DD）：");
        String date = scanner.nextLine();
        System.out.print("金额：");
        double amount = Double.parseDouble(scanner.nextLine());
        System.out.print("类别（如餐饮、交通等）：");
        String category = scanner.nextLine();
        System.out.print("备注：");
        String note = scanner.nextLine();
        try {
            // 调用BillService对象的recordExpense方法记录支出
            billService.recordExpense(date, amount, category, note);
            System.out.println("支出已成功记录！");
        } catch (SQLException e) {
            System.out.println("记录支出时发生错误：" + e.getMessage());
            System.out.println("按任意键返回主菜单..");
            scanner.nextLine();
        } catch (IllegalArgumentException e) {
            System.out.println("输入错误：" + e.getMessage());
            System.out.println("按任意键返回主菜单..");
            scanner.nextLine();
        }
    }

    // 查看所有账单方法
    private void viewAllBills() {
        try {
            // 调用BillService对象的viewAllBills方法获取所有账单
            List<Bill> bills = billService.getAllBills();
            // 遍历账单列表并输出
            for (Bill bill : bills) {
                System.out.println(bill);
            }
        } catch (SQLException e) {
            System.out.println("查看账单时发生错误：" + e.getMessage());
        } finally {
            System.out.println("按任意键返回主菜单..");
            scanner.nextLine();
        }
    }
    //根据月份设置月度预算
    private void setMonthlyBudget() {
        System.out.print("请输入每月预算：");
        double budget = Double.parseDouble(scanner.nextLine());
        try {
            // 调用BillService对象的setMonthlyBudget方法设置每月预算
            billService.setMonthlyBudget(budget);
            System.out.println("每月预算已成功设置！");
        } catch (SQLException e) {
            System.out.println("设置每月预算时发生错误：" + e.getMessage());
        }
    }
    //根据日期查询账单
    private void queryBills() {
        System.out.print("请输入查询日期（YYYY-MM-DD）：");
        String date = scanner.nextLine();
        try {
            // 调用BillService对象的queryBills方法查询指定日期的账单
            List<Bill> bills = billService.queryBills(date);
            // 遍历账单列表并输出
            for (Bill bill : bills) {
                System.out.println(bill);
            }
        } catch (SQLException e) {
            System.out.println("查询账单时发生错误：" + e.getMessage());
        }
    }

    //获取月度统计报告
    private void getMonthlyReport() {
        try {
            // 调用BillService对象的getMonthlyReport方法获取月度统计报告
            String report = billService.getMonthlyReport();
            System.out.println(report);
        } catch (SQLException e) {
            System.out.println("获取月度统计报告时发生错误：" + e.getMessage());
        }
    }
}